<template>
  <div class="app-container">
    <el-card class="search-card" shadow="never">
      <!-- 搜索操作 -->
      <template #header>
        <div class="card-header">
          <span>筛选条件</span>
          <el-button
            class="collapse-btn"
            type="text"
            @click="toggleSearch"
            :icon="showSearch ? 'ArrowDownBold' : 'ArrowUpBold'"
          >
            {{ showSearch ? "收起" : "展开" }}
          </el-button>
        </div>
      </template>
      <!-- 搜索框 -->
      <el-form
        :model="queryParams"
        ref="distributionLogRef"
        :inline="true"
        v-show="showSearch"
        label-width="88px"
      >
        <el-form-item label="日志流水号" prop="logCode">
          <el-input
            v-model="queryParams.logCode"
            placeholder="请输入日志流水号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="分销员" prop="distributorId">
          <el-input
            v-model="queryParams.distributorId"
            placeholder="请输入分销员ID"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="操作员" prop="adminId">
          <el-input
            v-model="queryParams.adminId"
            placeholder="请输入操作管理员ID"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="日志类型" prop="logType">
          <el-select
            v-model="queryParams.logType"
            placeholder="请选择日志类型"
            clearable
          >
            <el-option
              v-for="dict in log_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="操作动作说明" prop="action">
          <el-input
            v-model="queryParams.action"
            placeholder="请输入操作动作说明"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="变更对象" prop="changeObject">
          <el-input
            v-model="queryParams.changeObject"
            placeholder="请输入变更对象"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="来源单号" prop="sourceCode">
          <el-input
            v-model="queryParams.sourceCode"
            placeholder="请输入来源单号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="操作IP" prop="ipAddress">
          <el-input
            v-model="queryParams.ipAddress"
            placeholder="请输入操作IP"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
        <!-- <el-form-item label="用户代理" prop="userAgent">
          <el-input
            v-model="queryParams.userAgent"
            placeholder="请输入用户代理"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择状态"
            clearable
          >
            <el-option
              v-for="dict in wdm_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="版本号" prop="version">
          <el-input
            v-model="queryParams.version"
            placeholder="请输入版本号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 表单 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ruoyi:distributionLog:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyi:distributionLog:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyi:distributionLog:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ruoyi:distributionLog:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table
      v-loading="loading"
      border
      resizable
      :data="distributionLogList"
      show-overflow-tooltip="true"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="序号"
        type="index"
        min-width="60"
        align="center"
        prop="logId"
      />
      <el-table-column
        label="流水号"
        min-width="150"
        align="center"
        prop="logCode"
        show-overflow-tooltip="true"
      />
      <el-table-column label="分销员" align="center" prop="distributorId" />
      
      <el-table-column label="日志类型" align="center" prop="logType">
        <template #default="scope">
          <dict-tag :options="log_type" :value="scope.row.logType" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作说明"
        min-width="150"
        align="center"
        prop="action"
      />
      <el-table-column
        label="变更对象"
        min-width="150"
        align="center"
        prop="changeObject"
        show-overflow-tooltip="true"
      />
      <!-- <el-table-column label="变更详情" align="center" prop="changeDetail" /> -->
      <!-- <el-table-column label="来源单号" align="center" prop="sourceCode" /> -->
      <el-table-column label="操作IP" align="center" prop="ipAddress" />
      <el-table-column label="操作员" align="center" prop="adminId" />
      <!-- <el-table-column label="用户代理" align="center" prop="userAgent" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="wdm_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="版本号" align="center" prop="version" /> -->
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column
        label="操作"
        min-width="180"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyi:distributionLog:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyi:distributionLog:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分销操作日志对话框 -->
    <el-dialog :title="title" v-model="open" width="720px" append-to-body>
      <el-collapse v-model="activeCollapse">
        <!-- 分组内容 -->
        <!-- 基本信息 -->
        <el-collapse-item name="basic" title="基本信息">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="流水号" prop="logCode">
                <el-input
                  v-model="form.logCode"
                  placeholder="LOG开头"
                  @blur="validateLogCode"
                >
                  <template #prepend>LOG</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="分销员" prop="distributorId">
                <el-input
                  v-model="form.distributorId"
                  placeholder="请输入分销员ID"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="操作员" prop="adminId">
                <el-input
                  v-model="form.adminId"
                  placeholder="请输入操作管理员ID"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>

        <!-- 日志信息 -->
        <el-collapse-item name="log" title="日志信息">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="日志类型" prop="logType">
                <el-select v-model="form.logType" placeholder="请选择日志类型">
                  <el-option
                    v-for="dict in log_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="操作说明" prop="action">
                <el-input
                  v-model="form.action"
                  placeholder="请输入操作动作说明"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="变更对象" prop="changeObject">
                <el-input
                  v-model="form.changeObject"
                  placeholder="请输入变更对象"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="变更详情" prop="changeDetail">
                <el-input
                  v-model="form.changeDetail"
                  type="textarea"
                  rows="3"
                  placeholder="请输入变更详情"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="来源单号" prop="sourceCode">
                <el-input
                  v-model="form.sourceCode"
                  placeholder="请输入来源单号"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>

        <!-- 操作信息 -->
        <el-collapse-item name="operation" title="操作信息">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="操作IP" prop="ipAddress">
                <el-input v-model="form.ipAddress" placeholder="请输入操作IP" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="用户代理" prop="userAgent">
                <el-input
                  v-model="form.userAgent"
                  placeholder="请输入用户代理"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>

        <!-- 系统信息 -->
        <el-collapse-item name="system" title="系统信息">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="状态" prop="status">
                <el-radio-group v-model="form.status">
                  <el-radio
                    v-for="dict in wdm_status"
                    :key="dict.value"
                    :label="dict.value"
                    >{{ dict.label }}</el-radio
                  >
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="删除" prop="delFlag">
                <el-radio-group v-model="form.delFlag">
                  <el-radio
                    v-for="dict in del_flag"
                    :key="dict.value"
                    :label="dict.value"
                    >{{ dict.label }}</el-radio
                  >
                </el-radio-group>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="12">
              <el-form-item label="版本号" prop="version">
                <el-input
                  v-model="form.version"
                  placeholder="系统自动管理"
                  disabled
                />
              </el-form-item>
            </el-col> -->
            <el-col :span="24">
              <el-form-item label="备注信息" prop="remark">
                <el-input
                  v-model="form.remark"
                  type="textarea"
                  rows="3"
                  placeholder="请输入备注信息"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>
      </el-collapse>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="DistributionLog">
import {
  listDistributionLog,
  getDistributionLog,
  delDistributionLog,
  addDistributionLog,
  updateDistributionLog,
} from "@/api/ruoyi/distributionLog";

const { proxy } = getCurrentInstance();
const { log_type, wdm_status, del_flag } = proxy.useDict(
  "log_type",
  "wdm_status",
  "del_flag"
);

const distributionLogList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    logCode: null,
    distributorId: null,
    adminId: null,
    logType: null,
    action: null,
    changeObject: null,
    changeDetail: null,
    sourceCode: null,
    ipAddress: null,
    userAgent: null,
    status: null,
    version: null,
  },
  rules: {
    logCode: [
      { required: true, message: "日志流水号不能为空", trigger: "blur" },
      {
        pattern: /^LOG\d+$/,
        message: "日志流水号格式错误，必须以LOG开头后跟数字",
        trigger: "blur",
      },
    ],
    distributorId: [
      { required: true, message: "分销员ID不能为空", trigger: "blur" },
    ],
    adminId: [
      { required: true, message: "操作管理员ID不能为空", trigger: "blur" },
    ],
    logType: [
      {
        required: true,
        message: "日志类型不能为空",
        trigger: "change",
      },
    ],
    action: [
      { required: true, message: "操作动作说明不能为空", trigger: "blur" },
    ],
    ipAddress: [
      { required: true, message: "操作IP不能为空", trigger: "blur" },
      {
        pattern: /^(\d{1,3}\.){3}\d{1,3}$/,
        message: "请输入正确的IP地址格式",
        trigger: "blur",
      },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

const activeCollapse = ref(["basic", "log", "operation", "system"]);

function validateLogCode() {
  if (form.value.logCode && !form.value.logCode.startsWith("LOG")) {
    proxy.$modal.msgError("日志流水号必须以LOG开头");
    form.value.logCode = "LOG" + form.value.logCode.replace(/[^0-9]/g, "");
  }
}

// 新增：搜索区域折叠/展开
function toggleSearch() {
  showSearch.value = !showSearch.value;
}

/** 查询分销操作日志列表 */
function getList() {
  loading.value = true;
  listDistributionLog(queryParams.value).then((response) => {
    // distributionLogList.value = response.rows
    distributionLogList.value = response.rows.filter(
      (item) => item.delFlag !== "2"
    );
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    logId: null,
    logCode: null,
    distributorId: null,
    adminId: null,
    logType: null,
    action: null,
    changeObject: null,
    changeDetail: null,
    sourceCode: null,
    ipAddress: null,
    userAgent: null,
    status: null,
    delFlag: null,
    version: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
  };
  proxy.resetForm("distributionLogRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.logId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加分销操作日志";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _logId = row.logId || ids.value;
  getDistributionLog(_logId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改分销操作日志";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["distributionLogRef"].validate((valid) => {
    if (valid) {
      if (form.value.logId != null) {
        updateDistributionLog(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDistributionLog(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _logIds = row.logId || ids.value;
  proxy.$modal
    .confirm('是否确认删除分销操作日志编号为"' + _logIds + '"的数据项？')
    .then(function () {
      return delDistributionLog(_logIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ruoyi/distributionLog/export",
    {
      ...queryParams.value,
    },
    `distributionLog_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>

<style scoped>
/* 搜索卡片样式 */
.search-card {
  /* margin-bottom: 16px; */
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: bold;
  color: #303133;
}

.collapse-btn {
  padding: 0;
  font-size: 14px;
  color: #606266;
}
</style>
